<!-- 小测组件 -->
<template>
  <div v-show="showDialog" id="quiz-teacher">
    <div class="title-bar">
      <span class="text">测验</span>
      <span @click="closeDialog()" class="close">X</span>
    </div>
    <div class="title-body">
      <div class="placeholder"></div>
    </div>
  </div>
</template>

<script>
let eventEmitter = BJY.eventEmitter

export default {
  components: {},
  data () {
    return {
      quiz: null,
      showDialog: false
    }
  },
  computed: {},
  watch: {},
  methods: {
    closeDialog () {
      this.showDialog = false
    }
  },
  created () {
    let that = this
    eventEmitter
      .on(
        // 小测开始事件
        'ZWZ_TQUIZ',
        (e, data) => {
          // if(!!that.quiz){
          //   that.quiz.destroy()
          //   that.quiz = null
          // }
          that.showDialog = true
          if(!that.quiz){
            that.quiz = BJY.quizTeacher.init({
                element: $('#quiz-teacher .placeholder')[0],
                // 错误回调
                onError: function (data) {
                    alert(data.msg);
                },
                // 开始小测前回调，返回 true 会开始执行小测，返回false不会执行，可用于条件判断
                onBeforeStartQuiz: function () {
                    return true;
                },
                // 开始发布试卷前回调，返回 true 会执行发布，返回false不会执行，可用于条件判断
                onBeforePublishSolution: function () {
                    return true;
                }
            });
          }
        }
      )
  },
  mounted () {
    $('#quiz-teacher').on('click', 'a', function(){
      let _href = $(this).attr('href')
      if(/\.xlsx/.test(_href)){
        window.location.href = _href.replace(/^\/\//, 'https://')
        return false
      }
    })
  },
  beforeDestroy () { }
}
</script>

<style lang="less" scoped>
//@import url(); 引入公共css类
  .title-body{
    margin-top: 30px;
    height: 100%;
  }
  
  #quiz-teacher {
    position: absolute;
    z-index: 9999;
    width: 670px;
    height: 500px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
  }

  .title-bar {
    position: absolute;
    top: 0;
    height: 30px;
    width: 100%;
    background: #444c63;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    align-items: center;
    color: #fff;

    .close {
      cursor: pointer;
    }
  }

  .placeholder {
    position: absolute;
    top: 30px;
    width: 100%;
    bottom: 0;
  }
</style>
